/**
 * Created by zhoulin on 2019/4/3.
 */
var companyId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.companyId);
var schoolId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolId);
var schoolMap = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolMap);

var app = new Vue({
    el: '#app',
    data: {
        permissions:['coupon:add','coupon:update'],//本页权限数组
        selects:[],
        schoolId:schoolId,
        schoolList:schoolMap,
        unit:'元',
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        that.getDicList("couponType");
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if(empty(token)){
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon:5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            },1200);
            return ;
        }
    },
    methods: {
        //权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        selectType: function () {
            var that = this;
            var selects = that.selects;
            var val = $("#type").val();
            for (let i = 0; i < selects.length; i++) {
                if (val == selects[i].keyS) {
                    if (selects[i].keyValue.indexOf("折扣") != -1) {
                        that.unit = "折扣";
                    } else {
                        that.unit = "元";
                    }
                }
            }
        },
        getDicList: function(keyTable) {
            var that = this;
            Get(that,_HOST+_API.common_dic_dicList+"?keyTable="+keyTable)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if(response){
                        if(response.success){
                            that.selects = response.list;
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                });
        },
        // 添加
        add:function () {
            var that = this;
            var param = formToJson($("#Form").serialize());
            param['companyId'] = companyId;
            //console.log(param)
            Post(that,_HOST+_API.marketing_coupon_add,param,true,that.permissions[0])
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if(response){
                        if(response.success){
                            layer.msg(response.msg, {time: 1500, icon:1});
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                layer.closeAll();
                            }, 1500)
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                    $("#Form").data('bootstrapValidator').destroy();
                    $('#Form').data('bootstrapValidator', null);
                    Validator();
                })
        },
    },
});


$('#Form').data('bootstrapValidator', null);
Validator();
function Validator() {
    $('#Form').bootstrapValidator({
        fields: {
            name: {
                validators: {
                    notEmpty: {
                        message: '名称不能为空'
                    },
                    stringLength: {
                        max: 50,
                        message: '名称长度最多50个字符'
                    },
                }
            },
            type: {
                validators: {
                    notEmpty: {
                        message: '优惠卷分类不能为空'
                    },
                }
            },
            couponMoney: {
                validators: {
                    notEmpty: {
                        message: '优惠券面值不能为空'
                    },
                }
            },
            minUseMoney: {
                validators: {
                    notEmpty: {
                        message: '最低消费金额不能为空'
                    },
                }
            },
            useTerm: {
                validators: {
                    notEmpty: {
                        message: '使用期限天数不能为空'
                    },
                }
            },
            sort: {
                validators: {
                    notEmpty: {
                        message: '排序不能为空'
                    },
                }
            },
        }
    }).on('success.form.bv', function (e) {
        // Prevent form submission
        e.preventDefault();
        if(empty(aGET.id)){
            app.add();
        }
    });
}
